package com.zy.dingdong.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zy.dingdong.R;
import com.zy.dingdong.adapter.FriendListAdapter;
import com.zy.dingdong.adapter.NewFriendListAdapter;
import com.zy.dingdong.dao.FriendDao;
import com.zy.dingdong.entity.Friend;
import com.zy.dingdong.util.OkHttpUtil;
import com.zy.dingdong.view.SearchFriendFragment;

import java.util.ArrayList;
import java.util.Map;

public class FriendListActivity extends BaseActivity {

    private RecyclerView recyclerView;

    private RecyclerView newFriendRView;

    public FriendListAdapter adapter;

    //新朋友列表适配器
    private NewFriendListAdapter adapter2;

    public ArrayList<Friend> friendList;

    private ArrayList<Friend> newFriendList = new ArrayList<>();

    private TextView titleText;

    private ImageView hasNew;

    private TextView openSearchPanel;

    private RelativeLayout openNewFriendBtn;

    private FrameLayout searchFriendPanel;

    private Boolean newFriendPanelVisibility = false;

    public ImageView hasNewFriendRequestImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.friend_list);

        recyclerView = findViewById(R.id.friend_list_view);
        newFriendRView = findViewById(R.id.new_friend_list);

        //标题Title布局设置
        titleText = findViewById(R.id.title_text);
        titleText.setText("好友列表");
        hasNew = findViewById(R.id.has_new_message);
        hasNew.setVisibility(View.GONE);
        ImageView back = findViewById(R.id.back);
        back.setVisibility(View.GONE);

        openSearchPanel = findViewById(R.id.open_search_friend);
        openNewFriendBtn = findViewById(R.id.open_new_friend_panel);
        //搜索朋友页面
        searchFriendPanel = findViewById(R.id.search_friend_panel);
        //新朋友申请
        hasNewFriendRequestImage = findViewById(R.id.has_new_friend);
        hasNewFriendRequestImage.setVisibility(View.GONE);
        //将搜索好友页面隐藏
        searchFriendPanel.setVisibility(View.GONE);
        //新朋友列表隐藏
        newFriendRView.setVisibility(View.GONE);

        //准备数据集合,名称+头像的数据集合
        initDatas();


        //设置RecycleView适配器
        adapter = new FriendListAdapter(FriendListActivity.this,friendList,owner);
        adapter2 = new NewFriendListAdapter(FriendListActivity.this,newFriendList,owner);

        recyclerView.setAdapter(adapter);
        newFriendRView.setAdapter(adapter2);

        //LayoutManager,设置布局类型才会显示
        recyclerView.setLayoutManager(new LinearLayoutManager(FriendListActivity.this));
        newFriendRView.setLayoutManager(new LinearLayoutManager(FriendListActivity.this));

        //打开搜索好友面板
        openSearchPanel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openFragment(new SearchFriendFragment());
                //设置布局可见
                searchFriendPanel.setVisibility(View.VISIBLE);
            }
        });
        //打开好友申请列表。若隐藏就设置为可见，如果可见就隐藏
        openNewFriendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (newFriendPanelVisibility){
                    newFriendRView.setVisibility(View.GONE);
                    newFriendPanelVisibility = false;
                }else{
                    newFriendRView.setVisibility(View.VISIBLE);
                    newFriendPanelVisibility = true;
                }
            }
        });
    }

    //创建SearchFragment
    private void openFragment(Fragment fragment){
        //获取FragmentManager
        FragmentManager fragmentManager = getSupportFragmentManager();
        //通过FragmentManager获取开启事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //将search_friend_panel替换成好友搜索的Fragment
        transaction.replace(R.id.search_friend_panel,fragment);
        transaction.commit();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void initDatas() {
        FriendDao friendDao = new FriendDao();
        //获取好友列表
        friendList = friendDao.findFriendListByOwnerNetId(owner.getNetId());
        //获取新朋友列表
        getNewFriendRequest();
    }
    //获取新好友请求
    private void getNewFriendRequest(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String url = SERVER_URL + "/user/getNewFriendRequests/" + owner.getUsername();
                    String jsonStr = OkHttpUtil.synGet(url);
                    getNewFriendResponse(jsonStr);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private void getNewFriendResponse(final String jsonStr){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Map jsonMap = JSON.parseObject(jsonStr);
                final String message = (String) jsonMap.get("message");
                if (message.equals("success")) {
                    //进行本地展示即可，不进行存储
                    JSONArray newFriends = (JSONArray) jsonMap.get("data");
                    for (int i = 0; i < newFriends.size(); i++) {
                        JSONObject jsonObj = (JSONObject) newFriends.get(i);
                        Friend friend = new Friend();
                        friend.setName(jsonObj.getString("name"));
                        friend.setHeadImage(jsonObj.getString("imagePath"));
                        friend.setNetId(jsonObj.getIntValue("id"));
                        newFriendList.add(friend);
                    }
                }
                //如果列表有数据，则展示，否则不展示
                if (newFriendList.size()>0){
                    hasNewFriendRequestImage.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}
